package com.fudan.cfg.base;

import java.lang.reflect.InvocationTargetException;
import java.util.Map;

import com.fudan.cfg.action.Foreach;
import com.fudan.cfg.action.SaveToEs;
import com.fudan.cfg.trans.CombineNode;
import com.fudan.run.ctx.ActionContext;

public abstract class ActionDef extends TypeNode{

	public ActionDef(String name, String type, String target) {
		super(name, type,target);
	}

	public static ActionDef buildFromConfiguration(Map<String,Object> configuration,ActionContext actionContext) {
		String name = (String) configuration.get(NAME_TAG);
		String type = (String) configuration.get(TYPE_TAG);
		String target = (String) configuration.get(TARGET_TAG);
		Class<? extends ActionDef> actionParser = actionContext.getParser(type);
		if(actionParser != null) {
			try {
				return actionParser.getConstructor(String.class,String.class,String.class).newInstance(name,type,target).build(configuration);
			} catch (InstantiationException | IllegalAccessException | IllegalArgumentException
					| InvocationTargetException | NoSuchMethodException | SecurityException e) {
				throw new RuntimeException(String.format("action构建配置解析器出错，type: %s",type),e);
			}
		}
//		if("saveToEs".equals(type)) {
//			return new SaveToEs(name,type).build(configuration);
//		}
//		if("foreach".equals(type)) {
//			return new Foreach(name, type).build(configuration);
//		}
		throw new RuntimeException(String.format("action没有匹配的配置解析器，type: %s",type));
	}


	public abstract ActionDef build(Map<String,Object> configuration);
}
